<?

class ViewMailchimp	{

	/**
	 * Define MailChimp default url 
	 *
	 * @name 	url		MailChimp url 
	 */
	const url		= "http://mailchimp.com/";

	/**
	 * Define Mailchimp Developers url 
	 *
	 * @name 	devurl	Mailchimp developers url 
	 */
	const devurl	= "https://us2.admin.mailchimp.com/";
	
	/**
	 * Define Mailchimp version 
	 *
	 * @name 	version	Mailchimp API version 
	 */
	const version	= 1.3;


	function __construct()	{
		
#		Communicate with external mailchimp class
		require_once ROOT . 'engine/ext/mailchimp/MCAPI.class.php';
		
		$this->account = engine_get( "json:mailchimp" );

		

	}
	
	function login()	{
		
		if( !$this->account )
			return null;
		
#		setup connection with callback url
		$mailchimp = new MCAPI( $this->account->api );
		
#		Authorize
		// Currently handled by the API Class
		
		return $mailchimp;
	}

	function options() {
	
		$options = array( "mailchimp.campaigns.view"=> "Campaigns", "mailchimp.lists.view"=> "User Lists", "mailchimp.stats.view"=> "Statistics", "mailchimp.ecommerce.view"=> "Commercial" );
	
		return array( 'elements' => array(	UList::unsorted( $options, 'ulist-menu' )
		
		));
	}
	
	
	function info() {
	
		return array( 'content'=> "Info part." );
	}


	function dashboard()	{
	
		
		if( !$Mailchimp = $this->login() )
			 return $this->noAccountContent();
	
			
		return array( 'content'=> "Send a fresh newsletter:",
					  'elements'=> array(	Button::view( 'Quickstart Campaign', "mailchimp", "newCampaign", "black" ),
					  						Text::description( "… or look at the process of your current one:" ),
					  						Button::view( 'View your stats', "mailchimp", "statisctics" ),
					  						Text::description( "<br><br><h1>Quick-add User</h1>Simply enter user Name and e-mail adrees, then assign her or him to the appropiate list." ),
					  						Input::update( 'User name', 'usertest', array( 'key'=> 'mailchimp.usertest' )),
					  						Input::update( 'E-mail address', 'usertest', array( 'key'=> 'mailchimp.usertest' )),
					  						Input::update( 'List', 'usertest', array( 'key'=> 'mailchimp.usertest' )),
					  						Button::view( 'Add', "mailchimp", "addUser", "black" )
		));	
	}

	
	function noAccountContent()	{
		
		return array(	'content'	=> "<h1>MailChimp</h1>Your MailChimp account is not connected, yet.",
			 			'elements'	=> array( Button::view( 'Connect MailChimp', "mailchimp", "setupAccount" )));
	}
	
	function setupAccount()	{

		return array(	'content'=> "<h1>Connect to MailChimp</h1>First we need a special \"API key\". This is the authorisation you give to the Engine to communicate as your account.<br>No account yet? Create one <a href='http://eepurl.com/c53Xw' target='_blank'>here</a>!",
						'elements'	=> array(	Button::link( 'Go to Mailchimp Dev', self::devurl . 'account/api/' ),
												Text::description( "Log in, then click <strong>add a key</strong> and copy the new API Key:" ),
												Input::update( 'API Key', 'settings', array( 'key'=> 'mailchimp.api' )),
												Input::update( 'Mailchimp name', 'settings', array( 'key'=> 'mailchimp.name' )),
												Button::view( 'Finish', "mailchimp", "finishSetupAccount", "black" )
				));
	
	}
	
	function finishSetupAccount()	{
	
		return $this->dashboard();	
	}

}

?>